package it.xiuxian.homepage.activity

import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import it.xiuxian.homepage.R
import it.xiuxian.homepage.bean.Record
import it.xiuxian.homepage.bean.RecordDetailBean
import it.xiuxian.homepage.databinding.HomepageRizhiBinding
import it.xiuxian.homepage.presenter.QianDaoRecordActivityPresenter
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseActivity
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import java.util.ArrayList

@Route(path = ArouterAddress.QIANDAORECORDACTIVITY)
class QianDaoRecordActivity:BaseActivity<QianDaoRecordActivityPresenter>() {
   lateinit var adapter1: SlimAdapter
   lateinit var binding:HomepageRizhiBinding
    override val layoutId: Int
        get() = R.layout.homepage_rizhi
    override val presenter: QianDaoRecordActivityPresenter
        get() = QianDaoRecordActivityPresenter()

    override fun initView() {
        //super.initView()
        binding= getViewDataBinding<HomepageRizhiBinding>()
        binding.rv.layoutManager = LinearLayoutManager(this)
        adapter1 = SlimAdapter.create().register<Record>(R.layout.homepage_detail_item,
            object: SlimInjector<Record> {
                override fun onInject(
                    data: Record?,
                    injector: IViewInjector<out IViewInjector<*>>?
                ) {

                    injector!!
                        .text(R.id.tv_time,data!!.createdate)
                        .text(R.id.tv_num,data.gongxianzhi)

                }
            }).attachTo(binding.rv)
    }
var i=1
    override fun initEvent() {
        binding.smart.setOnLoadMoreListener {
            if(next!=-1){
                mPresenter!!.getDetail((++i).toString())
            }else{
                binding.smart.setNoMoreData(true)
            }

        }
        binding.smart.setOnRefreshListener {
            i=1
            mPresenter!!.getDetail(i.toString())
            binding.smart.finishRefresh()
        }
        binding.cons.setOnClickListener {
            finish()
        }
    }

    override fun initData(){
        mPresenter!!.getDetail("1")
    }
    var newList = ArrayList<Record>()
    var next=0
    fun setData(data: RecordDetailBean){
        if(data.list.isNotEmpty()){
            Log.e("newdata",data.toString())
            if(i==1){
                newList.clear()
                newList.addAll(data.list)
                adapter1.updateData(newList)
            }else if(i>1){
                newList.addAll(data.list)
                adapter1.updateData(newList)
            }
            next = data.next
        }
        binding.smart.finishLoadMore()
    }
}